package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.model.FeedFb;
import com.ndp.fb.rdb.model.FeedWe;
import com.ndp.fb.rdb.api.ProductFbFeedRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.util.DateFormatUtil;
import com.ndp.fb.util.SQLUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by Elvis on 2016/4/14.
 */
@Service
public class ProductFbFeedRdbServiceImpl  extends DaoTemplate implements ProductFbFeedRdbService {

    @Override
    public List<FeedFb> getProductFbFeedList() {

        String sql = "SELECT * FROM product_fb_feed where is_delete = 0";

        return getJdbcTemplate().query(sql.toString(),  DefaultRowMapper.autoRowMapper(FeedFb.class));
    }

    @Override
    public FeedWe getProductWeFeedByFbFeedId(Long feedId) {
        String sql = "SELECT \n" +
                "  pwf.*\n" +
                "FROM\n" +
                "  product_fb_feed pf,\n" +
                "  product_catalog pc,\n" +
                "  product_we_feed_catalog pwfc,\n" +
                "  product_we_feed pwf \n" +
                "WHERE pf.`product_catalog_id` = pc.`product_catalog_id` \n" +
                "  AND pc.`product_catalog_id` = pwfc.`product_catalog_id` \n" +
                "  AND pwfc.`we_feed_id` = pwf.id \n" +
                "  AND pf.`is_delete` = 0 \n" +
                "  AND pc.`is_delete` = 0 \n" +
                "  AND pwf.`is_delete` = 0 \n" +
                "  AND pf.`product_feed_id` = ? ";
          List list =  getJdbcTemplate().query(sql,new Object[]{feedId}, DefaultRowMapper.autoRowMapper(FeedWe.class));
          if(list != null && list.size()>0){
              return (FeedWe)list.get(0);
          }
        return null;
    }

    @Override
    public int update(Long fbFeedId, String uploadErrorLevel, String uploadErrors,String latestUploadEndTime) {
        String sql  = "update product_fb_feed set upload_error_level =?, upload_errors =?, latest_upload_end_time = ? where product_feed_id = ? ";
        return getJdbcTemplate().update(sql,new Object[]{uploadErrorLevel,uploadErrors,latestUploadEndTime,fbFeedId});
    }

    @Override
    public int update(Long fbFeedId, String reprocessUrl, String latestUploadEndTime) {
        String sql  = "update product_fb_feed set reprocess_url =?, latest_upload_end_time = ? where product_feed_id = ? ";
        return getJdbcTemplate().update(sql,new Object[]{reprocessUrl,latestUploadEndTime,fbFeedId});
    }

    @Override
    public void updateForCatalogId(Long fbCatalogId, String reprocessUrl) {
        String sql  = "update product_fb_feed set reprocess_url =?, latest_upload_end_time = ? where product_catalog_id = ? ";
         getJdbcTemplate().update(sql,new Object[]{reprocessUrl, String.valueOf(System.currentTimeMillis()),fbCatalogId});
    }



    @Override
    public FeedFb getProductFbFeedByFeedId(Long productFeedId) {
        String sql = "SELECT * FROM product_fb_feed WHERE product_feed_id  = ?";
        return findOne(sql,new Object[]{productFeedId}, DefaultRowMapper.autoRowMapper(FeedFb.class));
    }

    @Override
    public FeedFb getProductFbFeedByCatalogId(Long catalogId) {
        String sql = "SELECT * FROM product_fb_feed WHERE product_catalog_id  = ?";
        return findOne(sql,new Object[]{catalogId}, DefaultRowMapper.autoRowMapper(FeedFb.class));
    }

    @Override
    public void  deleteByProductCatalogId(List<Long> catalogIds) {
        if (!catalogIds.isEmpty()) {
            String sql = "DELETE FROM product_fb_feed WHERE product_catalog_id  in ("+ SQLUtil.appendPlaceholder(catalogIds.size())+")";
            getJdbcTemplate().update(sql,catalogIds.toArray());
        }
    }

    @Override
    public Long save(FeedFb feedFb) {
        return saveResultId(feedFb);
    }

    @Override
    public Long saveOrUpdate(FeedFb feedFb) {
        Long id;
        FeedFb one = getProductFbFeedByFeedId(feedFb.getFbFeedId());
        if (one == null) {
            id=save(feedFb);
        }else {
            id = one.getId();
            update(feedFb.getFbFeedId(), feedFb.getReprocessUrl(), feedFb.getLatestUploadEndTime());
        }
        return id;
    }


}
